/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2009 - 2012 Luca Mingardi.
 *
 * This file is part of jeeObserver.
 *
 * JeeObserver is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * JeeObserver is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */
package jeeobserver.server;

import java.io.Serializable;
import java.util.Date;
import jeeobserver.server.Statistics.DateStatistics;
import jeeobserver.server.Statistics.NumberRangeStatistics;
import jeeobserver.server.Statistics.SamplingStatistics;

/**
 * The Class HardDiskStatistics.
 *
 * @author Luca Mingardi
 * @version 4.0
 */
public abstract class HardDiskStatistics extends SamplingStatistics implements Serializable {

	 private static final long serialVersionUID = 40L;

	 private final DateStatistics dateStatistics;

	 private final NumberRangeStatistics totalBytesStatistics;

	 private final NumberRangeStatistics usableBytesStatistics;

	 private final NumberRangeStatistics freeBytesStatistics;

	 private final NumberRangeStatistics usedBytesStatistics;

	 HardDiskStatistics(Date date, int count, DateStatistics dateStatistics, NumberRangeStatistics totalBytesStatistics, NumberRangeStatistics usableBytesStatistics, NumberRangeStatistics freeBytesStatistics, NumberRangeStatistics usedBytesStatistics) {
			super(date, count);
			this.dateStatistics = dateStatistics;
			this.totalBytesStatistics = totalBytesStatistics;
			this.usableBytesStatistics = usableBytesStatistics;
			this.freeBytesStatistics = freeBytesStatistics;
			this.usedBytesStatistics = usedBytesStatistics;
	 }

	 HardDiskStatistics(Date date, HardDiskStatistics statistics) {
			this(date,
							statistics.getCount(),
							statistics.getDateStatistics(),
							statistics.getTotalBytesStatistics(),
							statistics.getUsableBytesStatistics(),
							statistics.getFreeBytesStatistics(),
							statistics.getUsedBytesStatistics());
	 }

	 void add(HardDiskStatistics statistics) {

			super.add(statistics.getCount());

			this.totalBytesStatistics.add(statistics.getTotalBytesStatistics());
			this.usableBytesStatistics.add(statistics.getUsableBytesStatistics());
			this.freeBytesStatistics.add(statistics.getFreeBytesStatistics());
			this.usedBytesStatistics.add(statistics.getUsedBytesStatistics());

			this.dateStatistics.add(statistics.getDateStatistics());
	 }

	 public DateStatistics getDateStatistics() {
			return dateStatistics;
	 }

	 public NumberRangeStatistics getTotalBytesStatistics() {
			return totalBytesStatistics;
	 }

	 public NumberRangeStatistics getUsableBytesStatistics() {
			return usableBytesStatistics;
	 }

	 public NumberRangeStatistics getFreeBytesStatistics() {
			return freeBytesStatistics;
	 }

	 public NumberRangeStatistics getUsedBytesStatistics() {
			return usedBytesStatistics;
	 }

	 public static long getSerialVersionUID() {
			return serialVersionUID;
	 }

	 public static final class TimeStatistics extends HardDiskStatistics {

			private static final long serialVersionUID = 40L;

			public TimeStatistics(Date date, HardDiskStatistics statistics) {
				 super(date, statistics);
			}
	 }

	 public static final class TotalStatistics extends HardDiskStatistics {

			private static final long serialVersionUID = 40L;

			private final int samplingPeriod;

			private final String name;

			private final NumberTrend totalBytesTrend;

			private final NumberTrend usableBytesTrend;

			private final NumberTrend freeBytesTrend;

			private final NumberTrend usedBytesTrend;

			public TotalStatistics(HardDiskStatistics statistics, String name, int samplingPeriod) {
				 super(statistics.getDate(), statistics);

				 this.samplingPeriod = samplingPeriod;

				 this.name = name;

				 this.totalBytesTrend = new NumberTrend();
				 this.totalBytesTrend.add(statistics.getDate(), statistics.getTotalBytesStatistics().getSum(), statistics.getCount());

				 this.usableBytesTrend = new NumberTrend();
				 this.usableBytesTrend.add(statistics.getDate(), statistics.getUsableBytesStatistics().getSum(), statistics.getCount());

				 this.freeBytesTrend = new NumberTrend();
				 this.freeBytesTrend.add(statistics.getDate(), statistics.getFreeBytesStatistics().getSum(), statistics.getCount());

				 this.usedBytesTrend = new NumberTrend();
				 this.usedBytesTrend.add(statistics.getDate(), statistics.getUsedBytesStatistics().getSum(), statistics.getCount());

			}

			@Override
			public void add(HardDiskStatistics statistics) {
				 super.add(statistics);

				 this.totalBytesTrend.add(statistics.getDate(), statistics.getTotalBytesStatistics().getSum(), statistics.getCount());
				 this.usableBytesTrend.add(statistics.getDate(), statistics.getUsableBytesStatistics().getSum(), statistics.getCount());
				 this.freeBytesTrend.add(statistics.getDate(), statistics.getFreeBytesStatistics().getSum(), statistics.getCount());
				 this.usedBytesTrend.add(statistics.getDate(), statistics.getUsedBytesStatistics().getSum(), statistics.getCount());
			}

			public void calculateTrendStatistics() {
				 this.totalBytesTrend.calculateTrendStatistics();
				 this.usableBytesTrend.calculateTrendStatistics();
				 this.freeBytesTrend.calculateTrendStatistics();
				 this.usedBytesTrend.calculateTrendStatistics();
			}

			public int getSamplingPeriod() {
				 return samplingPeriod;
			}

			public String getName() {
				 return name;
			}

			public NumberTrend getTotalBytesTrend() {
				 return totalBytesTrend;
			}

			public NumberTrend getUsableBytesTrend() {
				 return usableBytesTrend;
			}

			public NumberTrend getFreeBytesTrend() {
				 return freeBytesTrend;
			}

			public NumberTrend getUsedBytesTrend() {
				 return usedBytesTrend;
			}
	 }

	 public static final class Element extends Statistics.Element {

			private static final long serialVersionUID = 40L;

			private final String context;

			private final String path;

			public Element(int id, String context, String path) {
				 super(id);
				 this.context = context;
				 this.path = path;
			}

			public Element(String context, String path) {
				 super((context + "|" + path).hashCode());
				 this.context = context;
				 this.path = path;
			}

			/**
			 * Gets the path.
			 *
			 * @return the path
			 */
			public String getPath() {
				 return this.path;
			}

			/**
			 * Gets the context.
			 *
			 * @return the context
			 */
			public String getContext() {
				 return this.context;
			}
	 }
}
